package com.weixing.mall.pay.channel.ali.payment;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.weixing.mall.pay.PayParam;
import com.weixing.mall.pay.PayResult;
import com.weixing.mall.pay.channel.ali.AbstractAliPay;
import com.weixing.mall.pay.channel.ali.AliPaySet;
import com.weixing.mall.pay.channel.ali.AliTradeTypeEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName AliPayPc
 * @Description
 * @Author Small
 * @Date 2019/11/27 18:56
 * @Version 1.0
 **/
@Slf4j
public class AliPayApp extends AbstractAliPay {


    private AlipayTradeAppPayRequest alipayRequest;
    private String PRODUCT_CODE = "QUICK_MSECURITY_PAY";


    public AliPayApp(AliPaySet aliPaySet) {
        super(aliPaySet);
    }

    @Override
    public void initAliPayRequest() {
        AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
        alipayRequest.setNotifyUrl(aliPaySet.getNotifyUrl());
        this.alipayRequest = alipayRequest;
    }


    @Override
    public void setPayRequestParam(PayParam payParam) {
        this.alipayRequest.setBizContent(JSON.toJSONString(wrapperRequestData(payParam, PRODUCT_CODE)));
    }


    @Override
    public PayResult executePay() throws AlipayApiException {
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(alipayRequest);
        if (response.isSuccess()) {
            response.getBody();
            return PayResult.success(response.getBody());
        } else {
            String subCode = response.getSubCode();
            String subMsg = response.getSubMsg();
            return PayResult.fail(subCode, subMsg);
        }
    }

    @Override
    public AliTradeTypeEnum getTradeType() {
        return AliTradeTypeEnum.APP;
    }
}
